package com.lumlord.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpMessageConverterExtractor;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;

import com.lumlord.common.domain.Result;
import com.lumlord.common.shiro.config.JwtAuthFilter;



import java.util.Map;

@Component
public class RestTemplateUtil {

	public Result post(String url, Map<String, Object> paMap) {
		// 核心返回结果报文字符串
		// 复杂构造函数的使用
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		headers.add(JwtAuthFilter.TOOKEN_NAME, getTooken());
		Gson gson = new GsonBuilder()
				.setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();
		String json = gson.toJson(paMap).toString();

		HttpEntity<String> entity = new HttpEntity<String>(json, headers);
		Result returnJson = restTemplate.postForObject(addToken(url), entity, Result.class);
		return returnJson;
	}

	public Result get(String url) {
		// 利用容器实现数据封装，发送

		RequestCallback requestCallback = restTemplate.acceptHeaderRequestCallback(Result.class);
		HttpMessageConverterExtractor<Result> responseExtractor = new HttpMessageConverterExtractor<>(Result.class,
				restTemplate.getMessageConverters());
		Result obj = restTemplate.execute(addToken(url), HttpMethod.GET, requestCallback, responseExtractor);
		return obj;
	}
	
	private String addToken(String url) {
		if(url.indexOf("?")==-1) {
			return url+"?"+JwtAuthFilter.TOOKEN_NAME+"="+getTooken();
		}else {
			return url+"&"+JwtAuthFilter.TOOKEN_NAME+"="+getTooken();
		}
	}

	public Result delete(String url) {
		RequestCallback requestCallback = restTemplate.acceptHeaderRequestCallback(Result.class);
		HttpMessageConverterExtractor<Result> responseExtractor = new HttpMessageConverterExtractor<>(Result.class,
				restTemplate.getMessageConverters());
		Result obj = restTemplate.execute(addToken(url), HttpMethod.DELETE, requestCallback, responseExtractor);
		return obj;
	}

	public Result put(String url, Map<String, Object> paMap) {
		// Map<String,String> paramters = new HashMap<>();
		// paramters.put("id",id);
		// paramters.put("access_token",accessToken);
		// 请求头
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		Gson gson = new GsonBuilder()
				.setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();
		String json = gson.toJson(paMap).toString();
		// 发送请求
		HttpEntity<String> entity = new HttpEntity<>(json.toString(), headers);
		// put请求
		ResponseEntity<Result> resultEntity = restTemplate.exchange(url, HttpMethod.PUT, entity,Result.class, paMap);
		return resultEntity.getBody();
	}

	private String tooken = null;

	public void setTooken(String tooken) {
		this.tooken = tooken;
	}

	public String getTooken() {
		try{
			return RequestToBean.getRequest().getHeader(JwtAuthFilter.TOOKEN_NAME);
		}catch (Exception e) {
			return tooken;
		}
	}


	@Autowired
	private RestTemplate restTemplate;
	 
}